// pages/my_look/my_look.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jobList: [],//无用  占位
    jobDateList: [],
    jobDateMapList: {}, //key 'yyyy-mm-dd', value job list
    jobListMore: {page:1},

    resumeList: [],
    resumeDateList: [],
    resumeDateMapList: {},
    resumeListMore: {page:1},

    companyList: [],
    companyDateList: [],
    companyDateMapList: {},
    companyListMore: {page:1},

    shopList: [],
    shopDateList: [],
    shopDateMapList: {},
    shopListMore: {page:1},

    courseList: [],
    courseDateList: [],
    courseDateMapList: {},
    courseListMore: {page:1},

    postList: [],
    postDateList: [],
    postDateMapList: {},
    postListMore: { page: 1 },
    postText:'暂无更多',

    nav: 0

  },
  nav: function (e) {
    this.setData({
      nav: e.currentTarget.dataset.index
    })
  },
  // 点赞
  praise: function (e) {
    var that = this;
    var post_id = e.currentTarget.dataset.id;
    var courseList = that.data.postList;
    var index = e.currentTarget.dataset.index;
    var indexs = e.currentTarget.dataset.indexs;
    var user_id = app.getUserinfo(true).id;
    console.log(e.currentTarget.dataset)
    core.post({
      url: '/discover/praise',
      data: { post_id: post_id, user_id: user_id, type: 1 },
      success(res) {
        if (res.code == 1) {
          courseList[index][indexs].is_praise = !courseList[index][indexs].is_praise;
          if (courseList[index][indexs].is_praise) {
            courseList[index][indexs].post.praise_num++;
          } else {
            courseList[index][indexs].post.praise_num--;
          }
          that.setData({ postList: courseList });
        }
      }
    })
  },
  // 进入详情页
  post_detail: function (e) {
    var post_id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/post_detail/post_detail?post_id=' + post_id + '&type=' + type
    })
  },
  // 帖子
  get_discover_log:function(){
    var that = this;
    var postList = that.data.postList;
    var page = that.data.postListMore.page;
    core.post({
      url: '/discover/get_discover_log',
      data: { page: page, postList: postList },
      success(res) {
        if (res.code == 1) {
          that.setData({ postList: res.data, postText:'加载更多' })
        }else{
          that.setData({ postText: '没有更多' })
        }
      },
      fail(res){
        that.setData({ postText: '没有更多' })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = app.getUserinfo();
    this.fetchJob();
    if(user.group_id==1) {
      this.fetchJob();
      this.data.nav = 0;
    }else {
      this.data.nav = 5;
      this.fetchResume();
    }
    this.setData({user, nav:this.data.nav});
    this.fetchCompany();
    //this.fetchShop();
   // this.fetchCourse();
    //this.get_discover_log();
  },

  fetchResume() {
    util.fetch(this, 'xilujob.resume/my_look', {}, 'resumeListMore', 'resumeList', 'data', list=>{
      list.forEach(resume=>{
        let viewDate = util.formatDatetime(resume.view_time,'yyyy年m月d日');
        if(this.data.resumeDateList.indexOf(viewDate)===-1) {
          this.data.resumeDateList.push(viewDate);
        }
        let resumeList = this.data.resumeDateMapList[viewDate] || [];
        resumeList.push(resume);
        this.setData({
          resumeDateList: this.data.resumeDateList,
          ['resumeDateMapList.'+viewDate]: resumeList
        });
      });
      return [];
    });
  },

  fetchJob() {
    util.fetch(this, 'xilujob.job/my_look', {}, 'jobListMore', 'jobList', 'data', list=>{
      list.forEach(job=>{
        let viewDate = util.formatDatetime(job.view_time,'yyyy年m月d日');
        if(this.data.jobDateList.indexOf(viewDate)===-1) {
          this.data.jobDateList.push(viewDate);
        }
        let jobList = this.data.jobDateMapList[viewDate] || [];
        jobList.push(job);
        this.setData({
          jobDateList: this.data.jobDateList,
          ['jobDateMapList.'+viewDate]: jobList
        });
      });
      return [];
    });
  },

  fetchCompany() {
    util.fetch(this, 'xilujob.company/my_look', {}, 'companyListMore', 'companyList', 'data', list=>{
      list.forEach(company=>{
        let viewDate = util.formatDatetime(company.view_time,'yyyy年m月d日');
        if(this.data.companyDateList.indexOf(viewDate)===-1) {
          this.data.companyDateList.push(viewDate);
        }
        let companyList = this.data.companyDateMapList[viewDate] || [];
        companyList.push(company);
        this.setData({
          companyDateList: this.data.companyDateList,
          ['companyDateMapList.'+viewDate]: companyList
        });
      });
      return [];
    });
  },

  fetchShop() {
    util.fetch(this, 'labour/my_look', {}, 'shopListMore', 'shopList', 'shops', list=>{
      list.forEach(shop=>{
        let viewDate = util.formatDatetime(shop.viewed_at,'yyyy年m月d日');
        if(this.data.shopDateList.indexOf(viewDate)===-1) {
          this.data.shopDateList.push(viewDate);
        }
        let shopList = this.data.shopDateMapList[viewDate] || [];
        shopList.push(shop);
        this.setData({
          shopDateList: this.data.shopDateList,
          ['shopDateMapList.'+viewDate]: shopList
        });
      });
      return [];
    });
  },

  fetchCourse() {
    util.fetch(this, 'course/my_look', {}, 'courseListMore', 'courseList', 'courses', list=>{
      list.forEach(course=>{
        let viewDate = util.formatDatetime(course.viewed_at,'yyyy年m月d日');
        if(this.data.courseDateList.indexOf(viewDate)===-1) {
          this.data.courseDateList.push(viewDate);
        }
        let courseList = this.data.courseDateMapList[viewDate] || [];
        courseList.push(course);
        this.setData({
          courseDateList: this.data.courseDateList,
          ['courseDateMapList.'+viewDate]: courseList
        });
      });
      return [];
    });
  },

  onShareAppMessage: function (res) {
    if (res.from == 'button') {
      var index = res.target.dataset.index;
      var indexs = res.target.dataset.indexs;
      var type = res.target.dataset.type;
      var list = this.data.postList;
      var path = '/pages/post_detail/post_detail?post_id=' + list[index][indexs].post.id + '&type='+type;
      if (list[index][indexs].post.image.length > 0) {
        var url = 'https://xilu-images.oss-cn-shanghai.aliyuncs.com' + list[index][indexs].post.image[0]
      } else {
        var url = '/images/business_logo.png'
      }
    }
    return {
      title: list[index][indexs].post.content,
      path: path,
      imageUrl: url
    }
  },

  
  fetchPost: function () {
    var nav = this.data.nav;
    if (nav == 4){
      var postListMore = this.data.postListMore;
      postListMore.page++;
      this.setData({ postListMore: postListMore })
      this.get_discover_log()
    }
  },

});
